/*
 * @Author: luoqi
 * @Date: 2022-04-15 19:45:30
 * @LastEditors: luoqi
 * @LastEditTime: 2022-04-27 16:35:10
 * @Description: 
 */
/*
 * @Author: luoqi
 * @Date: 2022-04-01 14:07:18
 * @LastEditors: luoqi
 * @LastEditTime: 2022-04-12 19:46:28
 * @Description: 
 */
import React, { useState,useEffect } from 'react';
import { Form, Input, Button, Select, Modal, Upload, message,Radio } from 'antd';
import {connect} from 'react-redux'
import { CheckOutlined, PlusOutlined } from '@ant-design/icons';
import E from 'wangeditor'

import {delTab,changeActive} from '../../store/tabAction'

import axios from '../../utils/axios'
import PubSub from 'pubsub-js'
import qs from 'qs'
const { Option } = Select;


const Addknowledeg = (props) => {
  const {departmentTree,partTree} = props.types;
  const [inputBlog, setInputBlog] = useState({});
  useEffect(()=>{
      var editor = new E("#content1");
      let config = editor.customConfig ? editor.customConfig : editor.config;
      config.onchange = (html) => {
        setInputBlog({...inputBlog,bcontext:html,babstract:editor.txt.text()})
      };
      editor.create();
  },[])

  
  const onFinish = (values) => {
    console.log('Success:', values);
    let {name,depType,posType,status}=values;
    console.log('内容：',inputBlog.bcontext);
    //拼接tag
    let tag=partTree.find(item=>item.id==posType).name+','+departmentTree.find(item=>item.id==depType).name
    // 获取用户id
    let id=JSON.parse(sessionStorage.getItem('userinfo')).username;
    console.log(id);
    axios({
      url: '/experience/upsert',
      method: 'get',
      params:{
        content: inputBlog.bcontext,
        status,
        tag:tag,
        title:name,
        author:id,
        departmentPid:depType,
        partPid:posType
      }
    }).then(res => {
      console.log(res);
      if (res.code === '200') {
        //提示
        message.success('添加成功');
        //删除标签栏
        props.delTab('addexp')
        //激活父页面
        props.changeActive('experience')
        //更新父页面
        PubSub.publish('setNumExperience',5)
      }
    })
  };

  const onFinishFailed = (errorInfo) => {
    console.log('Failed:', errorInfo);
  };
  function handleChange(value) {
    console.log(`selected ${value}`);
  }
  return (
    <div>
      
        <Form
          name="basic"
          labelCol={{ span: 2 }}
          wrapperCol={{ span: 20 }}
          onFinish={onFinish}
          onFinishFailed={onFinishFailed}
          autoComplete="off"
          initialValues={{posType:partTree[0].id,depType:departmentTree[0].id,status:1}}
        >
          <Form.Item
            label="经验标题:"
            name="name"
            rules={[{ required: true, message: '请输入经验标题' }]}
          >
            <Input />
          </Form.Item>

          <Form.Item
            label="部位一级分类:"
            name="posType"
            style={{zIndex:10,position:'relative'}}
          >
            <Select  style={{ width: 120 }} onChange={handleChange}>
              {partTree.map(item=>{
                return <Option key={item.id} value={item.id}>{item.name}</Option>
              })}
            </Select>
          </Form.Item>
          <Form.Item
            label="科室一级分类:"
            name="depType"
          >
            <Select style={{ width: 120 }} onChange={handleChange}>
            {departmentTree.map(item=>{
                return <Option key={item.id} value={item.id}>{item.name}</Option>
              })}
            </Select>
          </Form.Item>
         
          <Form.Item
            label="内容:"
          >
            <div id='content1' style={{zIndex:0,position:'relative'}}></div>
          </Form.Item>
        <Form.Item
          label="状态"
          name="status"
        >
          <Radio.Group  >
            <Radio value={1}>可用</Radio>
            <Radio value={0}>不可用</Radio>
          </Radio.Group>
        </Form.Item>
          <Form.Item
          wrapperCol={{
            offset: 12,
            span: 2,
          }}
        >
          
          <Button type="primary" htmlType='submit' size='large' icon={<CheckOutlined />}>
            提交
          </Button>
        </Form.Item>
        </Form>
    </div>
  )
}

export default connect(
  state=>({types:state.types}),
  {delTab,changeActive}
)(Addknowledeg);
